package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;

@Repository
public class YiJiaoJLDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(YiJiaoJL yiJiaoJL){
		Session session=sessionFactory.getCurrentSession();
		session.save(yiJiaoJL);
		session.flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiaoJL> getAllJL(String yiJiaoId){
		String hql="from YiJiaoJL t where t.yiJiao.id=? order by t.caoZuoRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, yiJiaoId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getMyYJSPJL(User user){
		String hql="select t.yiJiao from YiJiaoJL t where t.user.id=? and t.yiJiao.liuChengLX=2 group by t.yiJiao order by t.yiJiao.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getMyTHSPJL(User user){
		String hql="select t.yiJiao from YiJiaoJL t where t.user.id=? and t.yiJiao.liuChengLX=1 group by t.yiJiao order by t.yiJiao.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	public YiJiaoJL lastGZGLY(YiJiao yiJiao){
		String hql="from YiJiaoJL t where t.huanJie.id=503 and t.yiJiao=? order by t.caoZuoRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, yiJiao);
		if(query.list().size()>0)
			return (YiJiaoJL) query.list().get(0);
		else return null;
	}
}
